/*
	菜单
*/
define(
	[
		'require'
	],
	function(require) {
		"use strict";

		var Menus = Ember.Object.extend(Ember.Evented, {
			render: function() {
				var menusConfig = this.menusConfig;
				var divContainer = this.container;

				if (!divContainer) {
					return;
				}

				var $menu = $('<div class="topo-menus"></div');
				$menu
					.css({
						display: "none",
						position: "absolute"
					})
					.appendTo(divContainer);

				var $dropdown = $('<div class="btn-group"></div');
				$dropdown.appendTo($menu);

				if (menusConfig[0]) {
					if (menusConfig.length != 1 && menusConfig[0].accessBy.indexOf(4) == -1) {
						if (this.defaultBtn) {
							var $dropdownDefaultBtn = $('<button class="btn btn-primary" style="width:28px; height:28px;"><i class="iconfont icon-xiangqing" style="font-size:24px;"></i></button>');
							$dropdown.append($dropdownDefaultBtn);
							$dropdown.$dropdownDefaultBtn = $dropdownDefaultBtn;
						}
					}
				}

				if (this.get("auth.isNormal")) {
					if (this.nodeType != 4) {
						if (this.defaultBtn && (this.defaultBtn.code !== 'DELETE_RELATION')) {
							//var $dropdownDefaultBtn = $('<button class="btn btn-primary"></button>');
							var $dropdownDefaultBtn = $('<button class="btn btn-primary" style="width:28px; height:28px;"><i class="iconfont icon-xiangqing" style="font-size:24px;"></i></button>');
							$dropdown.append($dropdownDefaultBtn);
							$dropdown.$dropdownDefaultBtn = $dropdownDefaultBtn;
						}
					}
				}

				if (this.defaultBtn && (this.defaultBtn.code === 'DELETE_RELATION') && (!this.get("auth.isNormal"))) {
					var $dropdownDefaultBtn = $('<button class="btn btn-primary" style="width:28px; height:28px; line-height:26px;"><i class="iconfont icon-guanbi" style="font-size:16px;"></i></button>');
					$dropdown.append($dropdownDefaultBtn);
					$dropdown.$dropdownDefaultBtn = $dropdownDefaultBtn;
				}

				if (menusConfig && menusConfig.length > 0 && this.defaultBtn.code !== 'DELETE_RELATION') {
					var $dropdownToggle = $('<button class="btn btn-primary dropdown-toggle" style="width:28px; height:28px; line-height:28px;"><i class="iconfont icon-autopeizhi" style="font-size:24px;"></i></button>');
					var $dropdownMenu = $('<ul class="dropdown-menu pull-right" role="menu"></ul>');

					menusConfig.forEach(function(config) {
						$dropdownMenu.append('<li><a href="#" data-menu-code="' + config.code + '">' + config.name + '</a></li>');
					});

					$dropdown.$dropdownToggle = $dropdownToggle;
					$dropdown.$dropdownMenu = $dropdownMenu;

					$dropdown.append($dropdownToggle).append($dropdownMenu);
					if (menusConfig.length == 1 && menusConfig[0].accessBy.indexOf(4) != -1) {
						$dropdown.addClass("menu-up-width");
					}
				}

				this.$menu = $menu;
				this.$dropdown = $dropdown;

				this._addEventListner();
			},

			_addEventListner: function() {
				var me = this;
				var $dropdown = this.$dropdown;
				if ($dropdown) {
					var $dropdownDefaultBtn = $dropdown.$dropdownDefaultBtn;
					var $dropdownToggle = $dropdown.$dropdownToggle;
					var $dropdownMenu = $dropdown.$dropdownMenu;

					if ($dropdownDefaultBtn) {
						$dropdownDefaultBtn.click(function(e) {
							e.stopPropagation();
							me.onItemClick && me.onItemClick(e, me.defaultBtn.code);
							me.trigger("itemClick", e, me.defaultBtn.code);
						});
					}

					if ($dropdownToggle) {
						$dropdownToggle.click(function(e) {
							e.stopPropagation();
							$dropdown.toggleClass("open");
						});
						$dropdownToggle.mouseleave(function(e) {
							var target = e.toElement;
							var isEnterDropDown = $(target).closest(".btn-group")[0] == me.$dropdown[0];

							if (!isEnterDropDown) {
								me.onmouseout && me.onmouseout(e);
								me.trigger("mouseout", e);
							}
						});
						$dropdownToggle.on("mousemove", function(e) {

							me.show();
						});

						$dropdownToggle.on("mouseover mousemove", stopPropagation);
					}

					if ($dropdownMenu) {
						$dropdownMenu.mouseleave(function(e) {
							var target = e.toElement;
							var isEnterDropToggle = $(target).closest(".btn-group")[0] == me.$dropdown[0];

							if (!isEnterDropToggle) {
								me.onmouseout && me.onmouseout(e);
								me.trigger("mouseout", e);
							}

							$dropdown.removeClass("open");
						});
						$dropdownMenu.on("mousemove", function(e) {
							me.show();
						});
						$dropdownMenu.on("click", "a", function(e) {
							e.preventDefault();
							$dropdown.removeClass("open");
							me.hide();
							me.onItemClick && me.onItemClick(e, $(this).data("menu-code"));
							me.trigger("itemClick", e, $(this).data("menu-code"));
						});

						$dropdownMenu.on("mouseover mousemove", stopPropagation);
						//防止点击菜单时, 节点面板进入拖拽状态
						$dropdownMenu.on("mousedown", "a", stopPropagation);
					}

					//修复这些事件冒泡到zrender rootElement上时, 
					//引起的一些bug. 例如, 
					//1,鼠标移到其他节点面板菜单上时,  很容易引起第一个节点高亮
					//2,从菜单快速移除节点时, 节点mouseout不会触发
					$dropdown.on("mouseover mousemove mouseout mousedown mousewheel", stopPropagation);
				}

				this.$menu.on("mouseout", function() {
					me.hide();
				})

				function stopPropagation(e) {
					e.stopPropagation();
				}
			},

			show: function() {
				clearTimeout(this.hideMenuTimer);
				var $menu = this.$menu;
				$menu && $menu.show();
			},

			hide: function() {
				var me = this;
				clearTimeout(this.hideMenuTimer);
				this.hideMenuTimer = setTimeout(function() {
					var $menu = me.$menu;
					var $dropdown = me.$dropdown;
					if ($menu) {
						$menu.hide();
						$dropdown.removeClass("open");
					}
				}, 100);
			},

			position: function(x, y) {
				this.x = x || 0;
				this.y = y || 0;
				var $menu = this.$menu;

				if ($menu.find(".dropdown-toggle").length == 0) {
					x = x + 20;
				}

				$menu && $menu
					.css({
						left: x - 60,
						top: y + 3
					});
			},

			scale: function(scaling) {
				this.scaling = scaling || 1;
			},

			isTargetInMe: function(target) {
				return $(target).closest(".topo-menus")[0] == this.$menu[0];
			},

			destroy: function() {
				this.$menu.remove();
				this.$menu = null;
				this.$dropdown = null;
			}
		});

		return Menus;
	}
);